package bb.lanxing.activity.sport;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.adapter.sport.SportFinishFeelingAdapter;
import bb.lanxing.calc.PaceHelper;
import bb.lanxing.devices.sync.DeviceSyncHelper;
import bb.lanxing.model.database.ITrackPoint;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.BiCiCoorConverter;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.DateUtil;
import bb.lanxing.utils.MapUtil;

public class SportFinishActivity extends BaseActivity {
    private int[] feelingIconList = {R.drawable.ic_sport_experience_relaxed, R.drawable.ic_sport_experience_easy,
            R.drawable.ic_sport_experience_suitable, R.drawable.ic_sport_experience_tired, R.drawable.ic_sport_experience_very_tired};


    private BaiduMap mBaiduMap;

    private LinearLayout afterSelected;
    private TextView feeling;
    private ImageView feelingIcon;

    private Workout workout;
    private List<LatLng> points = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sport_finish);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        TextureMapView textureMapView = findViewById(R.id.textureMapView);
        mBaiduMap = textureMapView.getMap();
        mBaiduMap.setOnMapLoadedCallback(() -> setMapCenterAndRange(points));

        TextView distance = findViewById(R.id.distance);
        TextView time = findViewById(R.id.time);
        TextView calorie = findViewById(R.id.calorie);
        TextView pace = findViewById(R.id.pace);
        TextView steps = findViewById(R.id.steps);
         final String[] feelingNameList = {
            getString(R.string.sport_training_perception_very_relaxed),
            getString(R.string.sport_training_perception_relaxed),
            getString(R.string.sport_training_perception_moderation),
            getString(R.string.sport_training_perception_little_tired),
            getString(R.string.sport_training_perception_very_tired)
        };
        LinearLayout beforeSelected = findViewById(R.id.beforeSelected);
        RecyclerView feelingList = findViewById(R.id.feelingList);
        feelingList.setLayoutManager(new GridLayoutManager(this, 5));
        SportFinishFeelingAdapter feelingAdapter = new SportFinishFeelingAdapter(feelingNameList);
        feelingAdapter.setOnItemClickListener(position -> {
            beforeSelected.setVisibility(View.GONE);
            afterSelected.setVisibility(View.VISIBLE);
            feeling.setText(feelingNameList[position]);
            feelingIcon.setImageResource(feelingIconList[position]);
            if (workout != null) {
                workout.feeling = position + 1;
                ContentValues contentValues = new ContentValues();
                contentValues.put("feeling", workout.feeling);
                workout.update(contentValues);
            }
        });
        feelingList.setAdapter(feelingAdapter);
        afterSelected = findViewById(R.id.afterSelected);
        feeling = findViewById(R.id.feeling);
        feelingIcon = findViewById(R.id.feelingIcon);

        long workoutId = getIntent().getLongExtra("workoutId", 0);
        workout = Workout.getById(workoutId);
        if (workout == null) {
            return;
        }

        MapUtil mapUtil = MapUtil.getInstance();
        mapUtil.init(textureMapView);

        List<ITrackPoint> trackpointList = workout.getByWorkoutForMap();
        List<Double> speeds = new ArrayList<>();
        for (ITrackPoint trackpoint : trackpointList) {
            if (trackpoint != null) {
                points.add(BiCiCoorConverter.earth2Baidu(trackpoint.getLatLng()));
                speeds.add(trackpoint.getSpeed());
            }
        }
        mapUtil.drawPolyline(points, speeds);

        distance.setText(CommonUtil.getFormatDistance(workout.getDistance()));
        time.setText(DateUtil.format(workout.getDuration() * 1000, 2));
        calorie.setText(Math.round((workout.getCalorie() * 1.0f) / 1000.0f) + "kcal");
        PaceHelper paceHelper = new PaceHelper();
        paceHelper.init(workout);
        pace.setText(paceHelper.getAvgPace());
        steps.setText(workout.getStep() + "");

        if (workout.feeling > 0) {
            beforeSelected.setVisibility(View.GONE);
            afterSelected.setVisibility(View.VISIBLE);
            feeling.setText(feelingNameList[workout.feeling - 1]);
            feelingIcon.setImageResource(feelingIconList[workout.feeling - 1]);
        }

        DeviceSyncHelper.syncWorkoutWithId(App.getContext(), workout.getId());
    }

    private void setMapCenterAndRange(List<LatLng> points) {
        double[] gps = MapUtil.getFourCornerGps(points);
        LatLng center = MapUtil.getMapCenter(gps);
        float zoomLevel = MapUtil.getMapZoom(gps);
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLngZoom(center, zoomLevel));
    }
}